package com.ruoyi.web.controller.channel;

import com.ruoyi.basedata.domain.PayKind;
import com.ruoyi.basedata.domain.WalletKind;
import com.ruoyi.basedata.service.IPayKindService;
import com.ruoyi.basedata.service.IWalletKindService;
import com.ruoyi.channel.domain.MpChannel;
import com.ruoyi.channel.domain.MpChannelMethod;
import com.ruoyi.channel.domain.MpChannelMethodRSP;
import com.ruoyi.channel.service.IMpChannelMethodService;
import com.ruoyi.channel.service.IMpChannelService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 *  信息操作处理
 * 
 * @author ruoyi
 * @date 2019-05-06
 */
@Controller
@RequestMapping("/channel/method")
public class MpChannelMethodController extends BaseController
{
    private String prefix = "channel/mpChannelMethod";
	
	@Autowired
	private IMpChannelMethodService mpChannelMethodService;
	@Autowired
    private IMpChannelService iMpChannelService;

	
	@GetMapping()
	public String mpChannelMethod()
	{
	    return prefix + "/mpChannelMethod";
	}
	
	/**
	 * 查询列表
	 */
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(MpChannelMethod mpChannelMethod)
	{
		startPage();
		List<MpChannelMethod> list = mpChannelMethodService.selectMpChannelMethodList(mpChannelMethod);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出列表
	 */
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(MpChannelMethod mpChannelMethod)
    {
		List<MpChannelMethod> list = mpChannelMethodService.selectMpChannelMethodList(mpChannelMethod);
		ExcelUtil<MpChannelMethod> util = new ExcelUtil<MpChannelMethod>(MpChannelMethod.class);
		return util.exportExcel(list, "channelMethodCopy");
    }
	
	/**
	 * 新增
	 */
	@GetMapping("/add/{channelId}")
	public String add(@PathVariable("channelId") Integer channelId, ModelMap mmap)
	{
		mmap.put("mpChannel",iMpChannelService.selectMpChannelById(channelId));
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存
	 */
	@Log(title = "渠道支付方法", businessType = BusinessType.INSERT)
	@PostMapping("/addMethod")
	@ResponseBody
	public AjaxResult addSave(MpChannelMethod mpChannelMethod,BigDecimal merchantPayRate)
	{
		// 判断当前渠道的支付方法是否存在   渠道标识--支付类型
		MpChannelMethod existMpChannelMethod = new MpChannelMethod();
		existMpChannelMethod.setChannelId(mpChannelMethod.getChannelId());
		existMpChannelMethod.setPayKindId(mpChannelMethod.getPayKindId());
		List<MpChannelMethod> mpChannelMethodList = mpChannelMethodService.selectMpChannelMethodList(existMpChannelMethod);
		if (mpChannelMethodList.size() > 0) {
			return AjaxResult.error("该渠道的支付类型已存在");
		}
		// 支付限额左不能大于或者等于支付限额右
		int flag = mpChannelMethod.getPayLimitLeft().compareTo(mpChannelMethod.getPayLimitRight());
		if (flag == 1 ){
			return AjaxResult.error("添加失败！支付限额范围不正确！<br/> 支付限额左 > 支付限额右");
		}else if (flag == 0) {
			return AjaxResult.error("添加失败！支付限额范围不正确！<br/> 支付限额左 = 支付限额右");
		}

		// 商户支付方法费率要大于渠道支付方法费率
		int compare = merchantPayRate.compareTo(mpChannelMethod.getPayRate());
		if (compare == -1 ){
			return AjaxResult.error("商户支付方法-费率要大于渠道支付方法的费率");
		}else if (compare == 0) {
			return AjaxResult.error("商户支付方法-费率要大于渠道支付方法的费率");
		}

		return mpChannelMethodService.insertMpChannelMethod(mpChannelMethod,merchantPayRate);
	}

	/**
	 * 修改
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Long id, ModelMap mmap)
	{
		MpChannelMethod mpChannelMethod = mpChannelMethodService.selectMpChannelMethodById(id);
		mmap.put("mpChannelMethod", mpChannelMethod);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存
	 */
	@Log(title = "", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(MpChannelMethod mpChannelMethod, Long oldPayKindId, Long oldWalletKindId)
	{		
		return mpChannelMethodService.updateMpChannelMethod(mpChannelMethod, oldPayKindId, oldWalletKindId);
	}
	
	/**
	 * 删除
	 */
	@Log(title = "", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(mpChannelMethodService.deleteMpChannelMethodByIds(ids));
	}


	/**
	 * 渠道支付方法状态修改
	 */
	@Log(title = "渠道支付方法", businessType = BusinessType.DELETE)
	@PostMapping("/changeStatus")
	@ResponseBody
	public AjaxResult changeStatus(MpChannelMethod mpChannelMethod) {
		return  toAjax(mpChannelMethodService.changeStatus(mpChannelMethod));
	}



	/**
	 * 钱包类型
	 */
	@PostMapping("/checkWalletKindIdUnique")
	@ResponseBody
	public String checkWalletKindIdUnique(MpChannelMethod mpChannelMethod) {
		return mpChannelMethodService.checkWalletKindIdUnique(mpChannelMethod.getId(),mpChannelMethod.getChannelId(),mpChannelMethod.getWalletKindId());
	}


	/**
	 * 拿到最大支付费率
	 */
	@PostMapping("/maxPayRate")
	@ResponseBody
	public BigDecimal maxPayRate(int channelId) {
		if (channelId == -1) {
			return mpChannelMethodService.maxPayRate();
		}else {
			return mpChannelMethodService.maxPayRateByChannelId(channelId);
		}
	}
}
